package com.ch.doudemo.activity;

import android.support.annotation.NonNull;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.ch.doudemo.R;
import com.ch.doudemo.adapter.JzvdVerticalViewPagerAdapter;
import com.ch.doudemo.widget.VerticalViewPager;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.jzvd.JZVideoPlayer;

public class JzvdPagerActivity extends AppCompatActivity {

    @BindView(R.id.jzvd_viewPager)
    VerticalViewPager verticalViewPager;
    @BindView(R.id.srl_page)
    SmartRefreshLayout srlPage;
    private List<String> urlList;

    private Unbinder mUnbinder;

    private JzvdVerticalViewPagerAdapter pagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jzvd_pager);
        mUnbinder = ButterKnife.bind(this);

        makeData();
        initView();
        addListener();
    }

    private void addListener() {
        srlPage.setEnableAutoLoadMore(false);
        srlPage.setEnableLoadMore(false);
        srlPage.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                srlPage.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        urlList.addAll(urlList);
                        pagerAdapter.setUrlList(urlList);
                        pagerAdapter.notifyDataSetChanged();

                        srlPage.finishLoadMore();
                    }
                }, 2000);

            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {

            }
        });
    }

    private void initView() {
        pagerAdapter = new JzvdVerticalViewPagerAdapter(getSupportFragmentManager());
        verticalViewPager.setVertical(true);
        verticalViewPager.setOffscreenPageLimit(10);
        pagerAdapter.setUrlList(urlList);
        verticalViewPager.setAdapter(pagerAdapter);

        verticalViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                JZVideoPlayer.releaseAllVideos();
                if (position == urlList.size() - 1) {
                    srlPage.setEnableAutoLoadMore(true);
                    srlPage.setEnableLoadMore(true);
                } else {
                    srlPage.setEnableAutoLoadMore(false);
                    srlPage.setEnableLoadMore(false);
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    private void makeData() {
        urlList = new ArrayList<>();
        urlList.add("http://1255550251.vod2.myqcloud.com/103cb269vodtranscq1255550251/62003ff05285890784523774331/v.f40.mp4");
        urlList.add("http://1255550251.vod2.myqcloud.com/103cb269vodtranscq1255550251/f70671555285890785770127834/v.f30.mp4");
        urlList.add("http://1255550251.vod2.myqcloud.com/103cb269vodtranscq1255550251/10aedb4b5285890793337300725/v.f30.mp4");
        urlList.add("http://1255550251.vod2.myqcloud.com/3952c4c1vodcq1255550251/0bf19dd35285890790259710935/qm6enMGetG0A.mp4");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mUnbinder != null) {
            mUnbinder.unbind();
        }
    }
}
